from flask import Flask,render_template,request,session,redirect,url_for,sessions
from flask.views import MethodView

app = Flask(__name__)
app.secret_key = 'sfsdfs'

@app.route('/')
def index():
    return 'Hello'

class LoginView(MethodView):
    def __jump(self,msg=None):
        return render_template('login.html',msg=msg)

    def get(self):
        msg = request.args.get('msg')
        return self.__jump(msg=msg)

    def post(self):
        uname = request.form.get('uname')
        pwd = request.form.get('pwd')

        if uname == 'sxt' and pwd =='123':
            session['uname'] = uname
            session['pwd'] = pwd
            return render_template('index.html')
        return self.__jump('用户名或密码错误')

app.add_url_rule('/login/',view_func=LoginView.as_view('login'))

@app.route('/index/')
def home():
    uname = session.get('uname')
    if not uname:
        return redirect(url_for('login',msg='请先登录'))
    return render_template('index.html')


if __name__ == '__main__':
    app.run(debug=True)